<?php
// Imaging
class WJG_Resource_Imaging
{
    // Variables
    private $img_input;
    private $img_output;
    private $img_src;
    private $format;
    private $quality = 80;
    private $x_input;
    private $y_input;
    private $x_output;
    private $y_output;
    private $resize;

    // Set image
    public function set_img($img)
    {
        // Find format
        $ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));
        // JPEG image
        if(is_file($img) && ( $ext == "JPG" OR $ext == "JPEG" OR $ext== "jpg" OR $ext == "jpeg"))
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromJPEG($img);
            $this->img_src = $img;
        }
        // PNG image
        elseif(is_file($img) && ($ext == "PNG" || $ext == "png"))
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromPNG($img);
            $this->img_src = $img;
        }
        // GIF image
        elseif(is_file($img) && ($ext == "GIF" ||$ext == "gif"))
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromGIF($img);
            $this->img_src = $img;
        }
        // Get dimensions
//         echo $img;
//         echo $this->img_input;
        $this->x_input = imagesx($this->img_input);
        $this->y_input = imagesy($this->img_input);
    }

    // Set maximum image size (pixels)
    public function set_size($max_x=800 , $max_y=800)
    {
//        echo $this->x_input;

        // Resize
        if($this->x_input > 800)
        {
            $a= $max_x / $max_y;
            $b= $this->x_input / $this->y_input;
            if ($a<$b)
            {
                $this->x_output = $max_x;
                $this->y_output = ($max_x / $this->x_input) * $this->y_input;
            }
            else
            {
                $this->y_output = $max_y;
                $this->x_output = ($max_y / $this->y_input) * $this->x_input;
            }
            // Ready
            $this->resize = TRUE;
        }
        // Don't resize      
        else { $this->resize = TRUE; }
    }
    // Set image quality (JPEG only)
    public function set_quality($quality)
    {
        if(is_int($quality))
        {
            $this->quality = $quality;
        }
    }
    // Save image
    public function save_img($path,$x)
    {
//        echo $path;
//        die;
        // Resize
        if($this->resize)
        {
            $this->img_output = ImageCreateTrueColor($x, $this->y_input*$x/$this->x_input);
//            Zend_debug::dump($this->img_output);
//            echo $x."|".$this->y_input*$x/$this->x_input;
//            die;
            if($this->x_input > $x){
            ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $x, $this->y_input*$x/$this->x_input, $this->x_input, $this->y_input);
            }else{
               $this->resize=FALSE;            }
            }
        
        // Save JPEG 
        if($this->format == "JPG" OR $this->format == "JPEG" OR $this->format == "jpeg" OR $this->format == "jpg")
        {
            if($this->resize) { imageJPEG($this->img_output, $path, $this->quality); }
            else { copy($this->img_src, $path); }
        }
        // Save PNG
        elseif($this->format == "PNG" || $this->format == "png")
        {
            if($this->resize) { imagePNG($this->img_output, $path); }
            else { 
                copy($this->img_src, $path); }
        }
        // Save GIF
        elseif($this->format == "GIF" || $this->format=="gif")
        {
            if($this->resize) { imageGIF($this->img_output, $path); }
            else { copy($this->img_src, $path); }
        }
    }
  


    // Get width
    public function get_width()
    {
        return $this->x_input;
    }
    // Get height
    public function get_height()
    {
        return $this->y_input;
    }
    // Clear image cache
    public function clear_cache()
    {
        @ImageDestroy($this->img_input);
        @ImageDestroy($this->img_output);
    }
}